from nodetypes.ntypes.GuestAccount.edit_preferences.libmml.base import EventHandler


class EventHandler(EventHandler):
    
    def redirectToUserPreferences(self, context):
        defaults = context.defaults
        arg = context.request.args
        account = context.account
        
        prefs = account.getPreferences(defaults)
        if prefs:
            locale = prefs.mLocale and unicode(prefs.mLocale[0]) or \
                args.has_key('locale') and unicode(args.get('locale')[0]) or \
                default.default_locale            
        else:
            locale = args.has_key('locale') and unicode(args.get('locale')[0]) or \
                default.default_locale
            
        profile = account.getProfile(context, locale)
        if profile:
            context.response_uri = context.urlFormatter.formatUrl(
                profile.rdf_getDomain(),
                True
            )
            context.event.data.status = 'user-redirect'
            return None, None
        else:
            from Ft.Server.Server import FtServerServerException
            from Ft.Server.Server import Error
            raise FtServerServerException(
                Error.NOT_SUPPORTED,
                reason="Unknown action and/or method: (%s, %s)" %
                (context.action, context.method)
            )